package cn.itsource.pethome.base.service.impl;



import cn.itsource.pethome.base.mapper.BaseMapper;
import cn.itsource.pethome.base.query.BaseQuery;
import cn.itsource.pethome.base.service.IBaseService;
import cn.itsource.pethome.base.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    
    @Autowired
    private BaseMapper<T> baseMapper;

    /*添加*/
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    /*修改*/
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /*通过id进行删除*/
    @Override
    @Transactional
    public void removeById(Long id) {
        baseMapper.del(id);
    }

    /*通过id进行查询*/
    @Override
    @Transactional
    public T loadbyid(Long id) {
        T t = baseMapper.selectById(id);
        return t;
    }

    /*查询所有的上级部门*/
    @Override
    @Transactional
    public List<T> loadall() {
        List<T> ts = baseMapper.selectAll();
        return ts;
    }

    /*分页查询*/
    @Override
    @Transactional
    public PageBean<T> loadPage(BaseQuery query) {
        //先判断是否有数据
        Integer totals = baseMapper.findCount(query);
        if (totals.equals(0L)) {
            return new PageBean<T>();
        }

        //通过mapper调用list查询所有
        List<T> ts = baseMapper.list(query);

        //直接返回PageBean<>的有参构造
        return new PageBean<>(totals, ts);
    }

    /*通过id进行批量的删除，*/
    @Override
    public void patchdelet(Long[] ids) {
        /*        for (Long id : ids) {
         *//*这里是循环id，然后通过id进行删除，也可以用id的数组，在mapper的xml中有foreach进行遍历*//*
            baseMapper.patchdelet(id);
        }*/
        baseMapper.patchdelet(ids);
    }
}
